# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=mathgl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.0.3
pkgrel=2
pkgdesc="Library for high-quality scientific graphics (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://mathgl.sourceforge.io/"
license=('spdx:GPL-3.0-or-later AND LGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-fltk"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-zlib")
         #"${MINGW_PACKAGE_PREFIX}-wxWidgets"
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz"
        "001-fix-cmake-files.patch"
        "002-fix-NULL-ambigous-call.patch")
sha256sums=('9bba9ee6a0f86d1b8f3f3ba0374d3cb776f772dbb6f1a01684ca6c0bd56204d6'
            '81c5ebc6ca9cbf301099d2f25679951e10fad988b51e080f9374f9a77b2edda4'
            'd165b01edd1cef504315c3503ac202be97f3c14d5d0732c982630a9a5f845f7c')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/001-fix-cmake-files.patch"
  patch -Np1 -i "${srcdir}/002-fix-NULL-ambigous-call.patch"
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  CXXFLAGS=" -Wno-deprecated-declarations" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_CXX_STANDARD=11 \
    "${extra_config[@]}" \
    -Denable-all-widgets=OFF \
    -Denable-double=ON \
    -Denable-fltk=ON \
    -Denable-gif=ON \
    -Denable-glut=ON \
    -Denable-gsl=ON \
    -Denable-hdf5=ON \
    -Denable-jpeg=ON \
    -Denable-mgl2=ON \
    -Denable-opengl=ON \
    -Denable-png=ON \
    -Denable-qt6=ON \
    -Denable-qt6asqt=ON \
    -Denable-openmp=OFF \
    "../${_realname}-${pkgver}"

    # -Denable-pdf=ON
    # -Denable-wx=ON

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  # fixup references in associated cmake modules
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _ff in "${pkgdir}"${MINGW_PREFIX}/lib/cmake/mathgl2/*.cmake; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING_LGPL \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING_LGPL
}
